package com.lyluly.coursemgmt.webmodule.controller;

import com.lyluly.coursemgmt.servicemodule.iService.IStudentService;
import com.lyluly.coursemgmt.servicemodule.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @author clc
 */
@Controller
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private IStudentService iStudentService;

    /**
     * 添加一个学生
     * @param student 学生实体类
     * @param response
     */
    @PostMapping
    public void addStudent(@RequestBody Student student, HttpServletResponse response){
        student.setCreatetime(new Date());
        iStudentService.addStudent(student);
    }

    /**
     * 根据学生ID删除一个学生
     * @param sId 学生ID
     * @param response
     */
    @DeleteMapping("/{sId}")
    public void removeStudent(@PathVariable String sId, HttpServletResponse response){
        iStudentService.removeStudent(sId);
    }

    /**
     * 根据学生ID修改一个学生
     * @param student 学生实体类
     * @param response
     */
    @PutMapping
    public void modifyStudent(@RequestBody Student student, HttpServletResponse response){
        iStudentService.modifyStudent(student);
    }

    /**
     * 根据学生ID查找一个学生
     * @param sId 学生ID
     * @return 返回相应学生
     */
    @GetMapping("/sId/{sId}")
    @ResponseBody
    public Student findStudentById(@PathVariable String sId){
        return iStudentService.findStudentById(sId);
    }

    /**
     * 根据学生姓名查找若干个学生
     * @param sName 学生姓名
     * @return 返回相应学生
     */
    @GetMapping("/sName/{sName}")
    @ResponseBody
    public List<Student> findStudentListByName(@PathVariable String sName){
        return iStudentService.findStudentListByName(sName);
    }

    /**
     * 查找所有学生
     * @return 返回所有学生
     */
    @GetMapping
    @ResponseBody
    public List<Student> findStudentList(){
        return iStudentService.findStudentList();
    }
}
